package view.micro
{
	import events.BattleEvent;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import model.macro.RangedWeapon;
	import model.micro.Battle;
	import model.micro.BoundingBox;
	import model.micro.FieldUnit;
	import util.MyMath;
	
	/**
	 * A sprite for a single field unit in a battle.
	 */
	public class FieldUnitSprite extends Sprite implements IMicroSprite
	{
		/** The FieldUnit this sprite represents */
		public var unit:FieldUnit;
		/** The sprite for the weapon the unit is holding */
		private var weapon:WeaponSprite;
		
		// TODO: Stagger overlapping unit sprites
		
		/**
		 * Create a new FieldUnitSprite for a unit in a battle.
		 * @param	unit	The field unit this represents.
		 */
		public function FieldUnitSprite(unit:FieldUnit)
		{
			if (unit == null)
				throw new ArgumentError("unit cannot be null");
			
			alpha = 0.4;
				
			this.unit = unit;
			this.weapon = new WeaponSprite(unit.weapon);
			weapon.x = -6;
			weapon.y = 7;
			addChild(weapon);
			
			var box:BoundingBox = unit.getBoundingBox();
			x = box.center_x;
			y = box.bottom;
			
			graphics.beginFill(unit.macroUnit.owner.color);
			graphics.lineStyle(1, 0x000000, 1);
			graphics.drawRect( -box.width / 2, 0, box.width, box.height);
			
			addEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}
		
		/**
		 * Dispatch selected event when mouse is released on me.
		 * @param	e
		 */
		private function mouseUp(e:MouseEvent):void 
		{
			dispatchEvent(new BattleEvent(BattleEvent.UNIT_SELECTED, null, this.unit));
		}
		
		/**
		 * Update the sprite to match the unit's state.
		 */
		public function update():void
		{
			var box:BoundingBox = unit.getBoundingBox();
			x = box.center_x;
			y = box.bottom;
		}
		
		/**
		 * @return	True if this unit is no longer needed.
		 */
		public function readyToRemove():Boolean
		{
			return unit.readyToRemove();
		}
	
	}

}